// pages/components/sticky/sticky.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    offsetTop: 0, // navDom 距离页面顶部的距离
    isNavFixed: false, // 是否吸顶
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onReady() {
      var that = this;
      var query = wx.createSelectorQuery();
      query
        .select("#navDom")
        .boundingClientRect(function (res) {
          that.setData({
            offsetTop: res.top,
          });
        })
        .exec();
    },
    onPageScroll(res) {
      const { scrollTop } = res || {};
      let isNavFixed = false;
      if (scrollTop > this.data.offsetTop) {
        isNavFixed = true;
      } else {
        isNavFixed = false;
      }
      this.setData({
        isNavFixed,
      });
    },
  },
});
